import { useRouter, useRoute } from 'vue-router'
import { useLoginStore } from '../stores/index.js'

export const useLayout = () => {
  const loginStore = useLoginStore()
  const router = useRouter()
  const route = useRoute()
  
  const user = loginStore.user
  
  // 格式化角色显示文本
  const formatRole = (role) => {
    const roleMap = {
      'user': '用户',
      'student': '学生',
      'school': '学校管理员',
      'platform': '平台管理员'
    }
    return roleMap[role] || role
  }
  
  // 查看个人信息
  const handleViewPersonalInfo = () => {
    const currentPath = route.path;
    if (currentPath.includes('/user/parent/')) {
      router.push('/user/parent/info');
    } else if (currentPath.includes('/user/teacher/')) {
      router.push('/user/teacher/info');
    } else {
      router.push('/user/parent/info');
    }
  }

  // 身份切换
  const identityCut = () => {
    const currentPath = route.path;
    if (currentPath.includes('/user/parent/')) {
      router.push('/user/teacher/home');
    } else if (currentPath.includes('/user/teacher/')) {
      router.push('/user/parent/home');
    } else {
      router.push('/user/parent/home');
    }
  };

  // 退出登录
  const handleLogout = (loginPath) => {
    loginStore.logout()
    router.push(loginPath)
  }
  
  return {
    user,
    formatRole,
    handleViewPersonalInfo,
    identityCut,
    handleLogout,
    router,
    route
  }
}